// pages/daka/daka.js
var util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag:false,
    type:'success',
    msg:'签到成功'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var date = new Date();
    console.log(date)
    var month = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    wx.showLoading({
      title: 'HOBO打卡中...',
    });
    // setInterval(function () {
    //   that.setData({
    //     flag: true,
    //     time: month + '-' + day + ' ' + hour + ':' + minutes
    //   }),
    //   wx.hideLoading()
    // }, 1500);

    var nowtime = util.formatTime(new Date()); 
    wx.request({
      url: 'http://192.168.1.6:8080/WeChatJSON//signInfo',
      data:{
        empno:options.empno,
        nowtime: nowtime
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success:function(res){
        console.log(res)
        if (res.data === null){
          console.log("该时间段没有打卡任务！"),
          that.setData({
            flag: true,
            type:'warn',
            msg:'该时间段没有打卡任务',
            time: month + '-' + day + ' ' + hour + ':' + minutes
          }),
          wx.hideLoading()
        }else{
          var id = res.data.id;
          var flag = res.data.flag;
          if (flag == 1){
            that.setData({
              flag: true,
              type: 'success_no_circle',
              msg: '已完成签到',
              time: ""
            }),
            wx.hideLoading()
          }else{
            wx.request({
              url: 'http://192.168.1.6:8080/WeChatJSON//insertSignInfo',
              data: {
                id: id
              },
              method: 'GET',
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                console.log(res.data)
                if (res.data == true) {
                  console.log("签到成功")
                  that.setData({
                    flag: true,
                    type: 'success',
                    msg: '签到成功',
                    time: month + '-' + day + ' ' + hour + ':' + minutes
                  }),
                    wx.hideLoading()
                }
              },
              fail: function (res) {
                console.log(res)
              }
            })
          }
        }
      },
      fail:function(res){
        console.log(res)
      }
    })


  },
})